package me.jiatao.mybatisstudy.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.jiatao.mybatisstudy.mapper.plus.UserRedPakMapperPlus;
import me.jiatao.mybatisstudy.mapper.tk.UserRedPakMapper;
import me.jiatao.mybatisstudy.pojo.UserRedPacket;
import me.jiatao.mybatisstudy.pojo.UserRedPacketPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@Repository
public class UserRedPakDao {

    @Autowired
    UserRedPakMapper userRedPakMapper;

    @Autowired
    UserRedPakMapperPlus userRedPakMapperPlus;

    public UserRedPacket selectOne(UserRedPacket userRedPacket) {
        return userRedPakMapper.selectOne(userRedPacket);
    }

    public UserRedPacketPlus selectOnePlus(int integer) {
        return userRedPakMapperPlus.selectById(integer);
    }

    public void inserRedPacketPlus(UserRedPacketPlus redPacketPlus) {
        userRedPakMapperPlus.insert(redPacketPlus);
    }

    /**
     * 条件查询
     *
     * @return
     */
    public List<UserRedPacketPlus> getIDMore5400() {
        return userRedPakMapperPlus.selectList(
                new QueryWrapper<UserRedPacketPlus>()
                        .gt("id", 5400)
                        .gt("user_id", 22)
        );
    }


    public List<UserRedPacketPlus> getIDMore5400Lambda() {
//        return userRedPakMapperPlus.selectList(
//                new QueryWrapper<UserRedPacketPlus>()
//                        .lambda()
//                        .gt(UserRedPacketPlus::getId, 5400)
//                        .gt(UserRedPacketPlus::getUserId, 22)
//        );

//        return userRedPakMapperPlus.selectList(
//                new LambdaQueryWrapper<UserRedPacketPlus>()
//                        .gt(UserRedPacketPlus::getId, 5400)
//                        .gt(UserRedPacketPlus::getUserId, 22)
//        );

        return userRedPakMapperPlus.selectList(
                new LambdaQueryWrapper<UserRedPacketPlus>()
                        .and(plus -> plus.eq(UserRedPacketPlus::getId, 4000))
        );
    }

    /**
     * 测试分页
     *
     * @param curr
     * @param size
     * @return
     */
    public List<UserRedPacketPlus> getIDByPage(int curr, int size) {
        IPage<UserRedPacketPlus> userRedPacketPlusIPage = userRedPakMapperPlus.selectPage(
                new Page<>(curr, size),
                new LambdaQueryWrapper<UserRedPacketPlus>()
                        .gt(UserRedPacketPlus::getId, 5000));

        return userRedPacketPlusIPage.getRecords();
    }

    /**
     * 测试全局更新拦截
     */
    public void updateTest() {
        UserRedPacketPlus redPacketPlus = new UserRedPacketPlus();
        redPacketPlus.setAmount(666.0);
        redPacketPlus.setVersion(1);

        //没有设置条件，全局更新，会拦截
        //userRedPakMapperPlus.update(redPacketPlus, null);
        //设置条件，不会拦截
        userRedPakMapperPlus.update(redPacketPlus, new QueryWrapper<UserRedPacketPlus>().eq("id", 5406));
    }

    /**
     * 乐观锁测试
     */
    public void updateByVersion() {
        UserRedPacketPlus redPacketPlus = new UserRedPacketPlus();
        redPacketPlus.setAmount(888.0);
        redPacketPlus.setVersion(0);
        userRedPakMapperPlus.update(redPacketPlus, new QueryWrapper<UserRedPacketPlus>().eq("id", 5405));
    }

    /**
     * 逻辑删除
     */
    public void logicDelete(){
        userRedPakMapperPlus.deleteById(5405);
    }
}
